import { createRouter, createWebHistory } from 'vue-router';
import { useMemberStore } from '@/stores'

const memberStore = useMemberStore()
const user = memberStore.getUserInfo()

const routes = [
  // 定义你的路由规则
  { path: '/' },
  { path: '/pages/login/login' },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 添加全局前置守卫
router.beforeEach((to, from, next) => {
  // 判断用户是否已登录
  const TOKEN = user?.access_token;

  if (to.path === '/pages/login/login') {
    // 如果目标路径是登录页，则直接放行
    next();
  } else if (!TOKEN) {
    // 如果用户未登录并且试图访问非登录页，则重定向到登录页
    next('/pages/login/login');
  } else {
    // 用户已登录，继续导航
    next();
  }
});

export default router;
